#include "List.h"
ListNode* BuyNode(LTDataType x)
{
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	if (node == NULL)
	{
		perror("malloc");
		exit(1);
	}
	node->data = x;
	node->next = node->prev = node;
	return node;
}
void ListP(ListNode* phead)
{
	ListNode* pcur = phead->next;
	while (pcur != phead)
	{
		printf("%d->", pcur->data);
		pcur = pcur->next;
	}
	printf("\n");
}
ListNode* ListInit()
{
	ListNode* list = BuyNode(-1);
	return list;
}
void LTPushBack(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListNode* node = BuyNode(x);
	node->prev = phead->prev;
	phead->prev->next = node;
	node->next = phead;
	phead->prev = node;
}
void ListPushFront(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListNode* newnode = BuyNode(x);
	ListNode* node = phead->next;
	phead->next = node->prev = newnode;
	newnode->prev = phead;
	newnode->next = node;
}
void ListPopBack(ListNode* phead)
{
	assert(phead);
	ListNode* node = phead->prev;
	phead->prev = node->prev;
	node->prev->next = phead;
	free(node);
	node = NULL;
}
void ListPopFront(ListNode* phead)
{
	assert(phead);
	ListNode* node = phead->next;
	node->next->prev = phead;
	phead->next = node->next;
	free(node);
	node = NULL;
}
ListNode* ListFind(ListNode* phead, LTDataType x)
{
	assert(phead);
	ListNode* node = phead->next;
	while (node != phead)
	{
		if (node->data == x)
			return node;
		node = node->next;
	}
	return NULL;
}
void ListInsert(ListNode* pos, LTDataType x)
{
	ListNode* newnode = BuyNode(x);
	newnode->prev = pos->prev;
	newnode->next = pos;
	pos->prev->next = newnode;
	pos->prev = newnode;
}
void ListErase(ListNode* pos)
{
	ListNode* pcur = pos;
	pcur->prev->next = pcur->next;
	pcur->next->prev = pcur->prev;
	free(pcur);
	pcur = NULL;
}